package com.boot2.core.scheduler;//package com.yoju.conf;

import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author zhangweilin
 * @ClassName: UrlConf
 * @Description: 与url有关的配置
 * @date 2017年12月14日 下午2:00:42
 */
@Data
//@Conditional(ScheduleConfConditional.class) //这个也可以
@ConfigurationProperties(prefix = "schedule")
@ConditionalOnProperty(value = "schedule.enable", havingValue = "true")
@Configuration
@Component
@CommonsLog
public class ScheduleConf {

    /**
     * 并发job处理入口
     */
    private String concurrentJobClass;

    /**
     * 非并发job处理入口
     */
    private String unConcurrentJobClass;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        return schedulerFactoryBean;
    }

    /**
     * @return
     */
    @Bean
    public ScheduleManager scheduleManager() throws ClassNotFoundException {
        Assert.notNull(concurrentJobClass, "需要配置schedule.concurrentJobClass，此为并发job处理入口");
        Assert.notNull(unConcurrentJobClass, "需要配置schedule.unConcurrentJobClass，此为非并发job处理入口");
        Class<?> concurrentJobClazz = Class.forName(concurrentJobClass);
        Class<?> unConcurrentJobClazz = Class.forName(unConcurrentJobClass);
        ScheduleManager scheduleManager = new ScheduleManager(concurrentJobClazz, unConcurrentJobClazz);
        return scheduleManager;
    }
}
